import RescacheMgr from "../../framework/inc/rescachemgr";
import * as THREE from "../../../three.module";

const WIDTH_SEGMENTS_ = 10;
const HEIGHT_SEGMENTS_ = 10;

export default class EffGround extends THREE.Object3D {
    constructor(gwidth, gheight, grange, textlist) {
        super();
        let inc = RescacheMgr.getInstance();
        let plan_material = new THREE.ShaderMaterial(
            {
                uniforms: this.init_uniform(grange, textlist),
                vertexShader: inc.getGLSL("ground_vertex"),
                fragmentShader: inc.getGLSL("ground_fragment")
            }
        );

        let plan_geo = new THREE.PlaneGeometry(gwidth, gheight, WIDTH_SEGMENTS_, HEIGHT_SEGMENTS_);
        let mesh = new THREE.Mesh(plan_geo, plan_material);
        mesh.rotation.x = -Math.PI / 2;
        this.add(mesh);
        this._geoemtry = plan_geo;
    }

    init_uniform(grange, textlist) {
        let uniforms = {
            "bumpScale": {
                type: "f",
                value: grange,
            },
            "texture1": {
                type: "t",
                value: textlist["texture1"],
            },
            "texture2": {
                type: "t",
                value: textlist["texture2"],
            },
            "texture3": {
                type: "t",
                value: textlist["texture3"],
            },
            "texturemap": {
                type: "t",
                value: textlist["texturemap"]
            },
            "heightmap": {
                type: "t",
                value: textlist["heightmap"]
            }
        }
        uniforms["texture1"].value.wrapS = uniforms["texture1"].value.wrapT = THREE.RepeatWrapping;
        uniforms["texture2"].value.wrapS = uniforms["texture2"].value.wrapT = THREE.RepeatWrapping;
        uniforms["texture3"].value.wrapS = uniforms["texture3"].value.wrapT = THREE.RepeatWrapping;
        return uniforms;
    }
}